package com.koron.device.zsjRemote.service;

import com.koron.bean.base.Response;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.sms.IMobileMessageSender;
import com.koron.common.core.util.sms.MobileMessageVO;
import com.koron.device.bean.Equipment;
import com.koron.device.mapper.EquipmentMapper;
import com.koron.device.zsjRemote.bean.CollaborationRecord;
import com.koron.device.zsjRemote.feign.AttachmentFeignService;
import com.koron.device.zsjRemote.mapper.ZsjRemoteMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ZsjRemoteServiceImpl implements ZsjRemoteService {

	@Autowired
	private AttachmentFeignService attachmentFeignService;

	@Autowired
	private IMobileMessageSender iMobileMessageSender;

	@Override
	@TaskAnnotation("getAttachmentById")
	public Equipment getAttachmentById(SessionFactory factory , Equipment equipmentWithKks,String languageType){
		return getEqipmentById(factory,equipmentWithKks.getId(),equipmentWithKks.getEncode(),languageType);
	}

	@Override
	@TaskAnnotation("queryByNameAndKKS")
	public Response<List<Equipment>> queryByNameAndKKS(SessionFactory factory,Equipment equipment){
		ZsjRemoteMapper mapper = factory.getMapper(ZsjRemoteMapper.class);
		Assert.isTrue(!StringUtils.isAllBlank(equipment.getEncode(),equipment.getName()),"设备kks编码和设备名称不能同时为空!");
		List<Equipment> equipments;
		equipments = mapper.queryByNameAndKKS(equipment);
		return Response.success(equipments);
	}

	@Override
	@TaskAnnotation("getTypeByKKS")
	public String getTypeByKKS(SessionFactory factory,String encode){
		ZsjRemoteMapper mapper = factory.getMapper(ZsjRemoteMapper.class);
		return mapper.getTypeByKKS(encode);
	}

	@Override
	@TaskAnnotation("queryCollaborationRecord")
	public Response<List<CollaborationRecord>> queryCollaborationRecord(SessionFactory factory){
		return Response.ok(factory.getMapper(ZsjRemoteMapper.class).queryCollaborationRecord());
	}

/*	@Override
	@TaskAnnotation("queryCollaborationRecordByWorker")
	public Response<List<CollaborationRecord>> queryCollaborationRecordByWorker(SessionFactory factory,String account, String name){
		ZsjRemoteMapper mapper = factory.getMapper(ZsjRemoteMapper.class);
		Assert.isTrue(!StringUtils.isAllEmpty(account, name),"查询账号,id,名称不能为空");
		List<CollaborationRecord> records = mapper.queryCollaborationRecordByWorker(account, name);
		return Response.ok(records);
	}*/

	@Override
	@TaskAnnotation("startCollaboration")
	public Response startCollaboration(SessionFactory factory, CollaborationRecord record){
		//第一步,保存协作基本信息
		ZsjRemoteMapper mapper = factory.getMapper(ZsjRemoteMapper.class);
		record.setCreateBy(StringUtils.isBlank(ThreadLocalContext.get().getAccount()) ? "test" : ThreadLocalContext.get().getAccount());
		record.setCreateByName(StringUtils.isBlank(ThreadLocalContext.get().getName()) ? "测试" : ThreadLocalContext.get().getName());
		record.setDeleteFlag(0);
		//Assert.isTrue(!StringUtils.isAllBlank(record.getCreateBy(),record.getCreateByName()),"协作发起人不能为空");
		record.setId(StringUtils.isBlank(record.getId()) ? CodeTools.getCode32() : record.getId());
		record.setTitle(StringUtils.isBlank(record.getTitle()) ? record.getCreateByName()+"发起的协作记录" : record.getTitle());
		record.setCreateTime(new Date());
		//record.setStatus(StringUtils.isBlank(record.getStatus()) ? "协作中" : record.getStatus());
		int i = mapper.saveCollaboration(record);
		Assert.isTrue(i > 0,"保存失败");
		record.getWorkers().forEach(item->{
			item.setId(StringUtils.isBlank(item.getId()) ? CodeTools.getCode32() : item.getId());
			item.setRecordId(record.getId());
		});
		Assert.isTrue(mapper.saveCollaborationWorker(record.getWorkers()) > 0 ,"保存失败");;
		return  Response.ok("发送成功");
	}

	@Override
	public int sendPhoneMessage(String phone, String messageContent) {

		MobileMessageVO message = new MobileMessageVO();
		message.setReceiverPhone(phone);
		message.setMessageContent(messageContent);
		int result = iMobileMessageSender.sendSingleMessage(message);
		return result;
	}

	@Override
	public Equipment getEqipmentById( SessionFactory factory ,String id, String encode,String languageType) {
		EquipmentMapper mapper = factory.getMapper(EquipmentMapper.class);
		Equipment equipment = null;
		Assert.isTrue(!StringUtils.isAllEmpty(id,encode),"id,KKS编码不能同时为空");
		if(StringUtils.isNotBlank(encode)){
			equipment = mapper.getByEncode(encode,languageType);
		}
		else if(StringUtils.isNotBlank(id)){
			equipment = mapper.getById(id,languageType);
		}
		Assert.notNull(equipment,"设备不存在");
		return equipment;
	}
}
